#include "Avalon/IOTests/BufferReaderTester.hpp"
#include "Avalon/IO/Buffer.hpp"
#include "Avalon/IO/BufferReader.hpp"

using namespace Avalon;
using namespace Avalon::IO;
using namespace Avalon::IO::Tests;
using namespace boost;
using namespace boost::posix_time;
using namespace std;

void BufferReaderTester::TestCreateEmpty() {
  BufferReader reader("");
  CPPUNIT_ASSERT(reader.IsAtEndOfDataSource());
}

void BufferReaderTester::TestCreateNotEmpty() {
  string message = "hello world";
  BufferReader reader(message);
  CPPUNIT_ASSERT(!reader.IsAtEndOfDataSource());
}

void BufferReaderTester::TestRead() {
  string message = "hello world";
  BufferReader reader(message);
  Buffer data;
  CPPUNIT_ASSERT(reader.Read(&data)->Get(seconds(1)) == message.size());
  CPPUNIT_ASSERT(strncmp(data.GetData(), message.c_str(), message.size()) == 0);
}

void BufferReaderTester::TestReadSomeToBuffer() {
  string message = "hello world";
  BufferReader reader(message);
  Buffer data;
  CPPUNIT_ASSERT(reader.Read(&data, 6)->Get(seconds(1)) == 6);
  CPPUNIT_ASSERT(strncmp(data.GetData(), "hello ", 6) == 0);
  data.Reset();
  CPPUNIT_ASSERT(reader.Read(&data, 5)->Get(seconds(1)) == 5);
  CPPUNIT_ASSERT(strncmp(data.GetData(), "world", 5) == 0);
}

void BufferReaderTester::TestReadSomeToPointer() {
  string message = "hello world";
  BufferReader reader(message);
  char* data = new char[message.size()];
  CPPUNIT_ASSERT(reader.Read(data, 6)->Get(seconds(1)) == 6);
  CPPUNIT_ASSERT(strncmp(data, "hello ", 6) == 0);
  CPPUNIT_ASSERT(reader.Read(data, 5)->Get(seconds(1)) == 5);
  CPPUNIT_ASSERT(strncmp(data, "world", 5) == 0);
  delete[] data;
}
